/**
 * 自定义函数
 * */


var formList = {
    data: [],
    pages: {start: 0, limit: 10}
}; //数据列表


//列表
function getIndex(){
    var formData = {
        aid: getAid(),
        shopCode: shopCode,
        pageStart: formList.pages.start,
        pageLimit: formList.pages.limit
    };
    console.log(formData);
    $.ajax({
        type: apis.goods1Index.method,
        url: domain + apis.goods1Index.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                if (formList.pages.start > 0) {
                    msgPop({msg: res.msg});
                }
                return
            }
            for (var i = 0; i < res.data.length; i ++) {
                formList.data.push(res.data[i]);
            }
            formList.pages.start ++;
            getHtml();
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//列表字符串
function getHtml(){
    var h = '', i, d = formList.data;
    var id = 0, img = '', lasttime = '';
    h += '';
    for (i = 0; i < d.length; i ++) {
        id = d[i].id;
        img = d[i].img;
        lasttime = d[i].uptime==null ? '' : d[i].uptime;
        h += '<tr class="text-c">';
        h += '<td>' + id + '</td>';
        h += '<td>' + d[i].gname + '</td>';
        h += '<td>' + d[i].gcode + '</td>';
        h += '<td>' + d[i].price / 100 + '</td>';
        if (img == '') {
            h += '<td>-</td>';
        } else {
            h += '<td><img src="' + img + '" width="50"/></td>';
        }
        h += '<td>' + d[i].follow + '</td>';
        h += '<td>' + d[i].addtime + '</td>';
        h += '<td>' + lasttime + '</td>';
        h += '<td class="td-manage">';
        h += '<a href="javascript:;" title="编辑" class="ml-5" onclick="viewEdit(' + id + ');">';
        h += '编辑<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        //h += '&nbsp;';
        //h += '<a href="javascript:;" title="编辑" class="ml-5" onclick="imgEdit(' + id + ');">';
        //h += '图集<i class="Hui-iconfont">&#xe6df;</i>';
        //h += '</a>';
        h += '</td>';
        h += '</tr>';
    }
    h += '';
    $('#formList').html(h);
    $('#tableMore').show();
}


//表单新增
function viewAdd(){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var h = '';
    h += '';
    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.2; position: fixed; top: 0; left: 0; z-index: 10000;"></div>';
    h += '<div style="padding: 10px 20px; width: ' + (cw * 0.6) + 'px; text-align: center; background: #fff; color: #333; border-radius: 5px; position: fixed; top: 5%; left: ' + ((cw * 0.5 - 40) / 2) + 'px; z-index: 10000;">';
    h += '<div style="text-align: center; font-weight: bolder; font-size: 20px;">新增</div>';
    h += '<form class="form form-horizontal" style="padding: 20px; max-height: 500px; overflow-x: hidden; overflow-y: auto;">';
    //名称
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>商品名：</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<input type="text" class="input-text" placeholder="输入商品名" id="gname" value=""/>';
    h += '</div>';
    h += '</div>';
    //描述
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-2">商品描述：</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<textarea class="textarea"  placeholder="说点什么...最少输入10个字符" datatype="*10-100" dragonfly="true" id="describe"></textarea>';
    h += '</div>';
    h += '</div>';
    //单价
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>单价分：</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<input type="text" class="input-text" placeholder="输入商品单价" id="price" value="0"/>';
    h += '</div>';
    h += '</div>';
    //头像
    h += '<style type="text/css"></style>';
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-2">商品主图：</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<div style="padding: 5px 0; width: 100px; background: #0088cc; text-align: center; font-size: 12px; color: #fff; border-radius: 5px; position: relative;">重新上传图片</div>';
    h += '<input type="file" id="uploadFile" style="width: 180px; height: 30px; opacity: 0.1; cursor: pointer; position: relative; top: -30px; left: -180px;" onchange="uploadImage()"/>';
    h += '<div id="img" style="margin-top: -20px; width: 100px; height: 100px; background: #eee; overflow: hidden;"></div>';
    h += '</div>';
    h += '</div>';
    //按钮
    h += '<div class="row cl">';
    //h += '<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">';
    h += '<div class="col-xs-8 col-sm-9 col-xs-offset-3 col-sm-offset-2">';
    h += '<button class="btn btn-default radius" type="button" onClick="$(\'#formPop\').html(\'\');">&nbsp; 取 消 &nbsp;</button> <button class="btn btn-secondary radius" type="button" onClick="goodsAdd();"><i class="Hui-iconfont">&#xe632;</i> 保 存 </button>';
    h += '</div>';
    h += '</div>';
    h += '</form>';
    h += '</div>';
    h += '</div>';
    h += '';
    $('#formPop').html(h);
}


//显示图片
function showImg(e){
    var h = '';
    h += '<img src="' + e + '" style="width: 150px;"/>';
    h += '<input type="hidden" id="imgLink" value="' + e + '"/>';
    //h += '<a href="javascript:;" style="padding: 5px 10px; color: #fff; background: red; font-size: 12px; border: 0; border-radius: 0; opacity: 0.7; position: relative; top: -110px; left: 0;" onclick="imgDel();">点击删除</a>';
    $('#img').html(h);
}


//新增商品
function goodsAdd(){
    var gname = $('#gname').val();
    var describe = $('#describe').val();
    var img = $('#imgLink').val();
    var price = $('#price').val();
    if (gname.length < 2) {
        msgPop({msg: '商品名不能少于2位'}); return
    }
    if (price == 0) {
        msgPop({msg: '单价必填'}); return
    }
    if (img == undefined) {
        img = '';
    }
    var formData = {
        aid: getAid(),
        shopCode: shopCode,
        gname: gname,
        describe: describe,
        img: img,
        price: price
    };
    console.log(formData);
    $.ajax({
        type: apis.goods1Add.method,
        url: domain + apis.goods1Add.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '添加成功'});
            window.location.href = '';
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//表单修改
function viewEdit(v){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var i, goodsOne = '';
    for (i = 0; i < formList.data.length; i ++) {
        if (formList.data[i].id == v) {
            goodsOne = formList.data[i];
        }
    }
    if (goodsOne == '') {
        msgPop({msg: '商品不存在'}); return
    }
    console.log(goodsOne);
    var h = '';
    h += '';
    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.2; position: fixed; top: 0; left: 0; z-index: 10000;"></div>';
    h += '<div style="padding: 10px 20px; width: ' + (cw * 0.6) + 'px; text-align: center; background: #fff; color: #333; border-radius: 5px; position: fixed; top: 5%; left: ' + ((cw * 0.5 - 40) / 2) + 'px; z-index: 10000;">';
    h += '<div style="text-align: center; font-weight: bolder; font-size: 20px;">修改</div>';
    h += '<form class="form form-horizontal" style="padding: 20px; max-height: 500px; overflow-x: hidden; overflow-y: auto;">';
    //名称
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>商品名称：</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<input type="text" class="input-text" placeholder="输入商品名" id="gname" value="' + goodsOne.gname + '"/>';
    h += '<input type="hidden" id="goodsCode" value="' + goodsOne.gcode + '"/>';
    h += '</div>';
    h += '</div>';
    //描述
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-2">商品描述：</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<textarea class="textarea"  placeholder="说点什么...最少输入10个字符" datatype="*10-100" dragonfly="true" id="describe">' + goodsOne.describe + '</textarea>';
    h += '</div>';
    h += '</div>';
    //单价
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>单价分：</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<input type="text" class="input-text" placeholder="输入商品单价" id="price" value="' + goodsOne.price + '"/>';
    h += '</div>';
    h += '</div>';
    //头像
    h += '<style type="text/css"></style>';
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-2">商品主图：</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<div style="padding: 5px 0; width: 100px; background: #0088cc; text-align: center; font-size: 12px; color: #fff; border-radius: 5px; position: relative;">重新上传图片</div>';
    h += '<input type="file" id="uploadFile" style="width: 180px; height: 30px; opacity: 0.1; cursor: pointer; position: relative; top: -30px; left: -180px;" onchange="uploadImage()"/>';
    h += '<div id="img" style="margin-top: -20px; width: 100px; height: 100px; background: #eee; overflow: hidden;">';
    if (goodsOne.img != '') {
        h += '<img src="' + goodsOne.img + '" style="width: 150px;"/>';
    }
    h += '<input type="hidden" id="imgLink" value="' + goodsOne.img + '"/>';
    h += '</div>';
    h += '</div>';
    h += '</div>';
    //按钮
    h += '<div class="row cl">';
    h += '<div class="col-xs-8 col-sm-9 col-xs-offset-3 col-sm-offset-2">';
    h += '<button class="btn btn-default radius" type="button" onClick="$(\'#formPop\').html(\'\');">&nbsp; 取 消 &nbsp;</button> <button class="btn btn-secondary radius" type="button" onClick="goodsUpdate();"><i class="Hui-iconfont">&#xe632;</i> 保 存 </button>';
    h += '</div>';
    h += '</div>';
    h += '</form>';
    h += '</div>';
    h += '</div>';
    h += '';
    $('#formPop').html(h);
}


//更新
function goodsUpdate(){
    var gcode = $('#goodsCode').val();
    var gname = $('#gname').val();
    var describe = $('#describe').val();
    var img = $('#imgLink').val();
    var price = $('#price').val();
    if (gname.length < 2) {
        msgPop({msg: '商品名不能少于2位'}); return
    }
    if (price == 0) {
        msgPop({msg: '单价必填'}); return
    }
    if (img == undefined) {
        img = '';
    }
    var formData = {
        aid: getAid(),
        gcode: gcode,
        gname: gname,
        describe: describe,
        img: img,
        price: price
    };
    console.log(formData);
    $.ajax({
        type: apis.goods1Modify.method,
        url: domain + apis.goods1Modify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '修改成功'});
            window.location.href = '';
        },
        error: function () {
            console.log('网络错误');
        }
    });
}